﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;
using System.Data.SqlClient;
using Entity;
namespace Data
{
    public class Province_Data
    {
        SqlConnection con;
        SqlCommand comn;
        SqlDataAdapter da;
        DataSet ds;



        public bool Insert_Province(Province_Entity province)
        {
            try
            {
                con = Connection.StartConnection();

                comn = new SqlCommand("ProAdd", con);
                con.Open();
                comn.CommandType = CommandType.StoredProcedure;
                comn.Parameters.Add(new SqlParameter("@provinceName", province.Provincename));
                comn.Parameters.Add(new SqlParameter("@pricehcmc", province.Price));
                comn.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Connection.CloseConnection();
            }

            return true;
        }

     

        public int Update_Province(int provinceID, string provinceName, int pricehcmc)
        {
            con = Connection.StartConnection();
            con.Open();
            comn = new SqlCommand("UpdatePro", con);
            comn.CommandType = CommandType.StoredProcedure;
            try
            {
                comn.Parameters.AddWithValue("@provinceID", provinceID);
                comn.Parameters.AddWithValue("@provinceName", provinceName);
                comn.Parameters.AddWithValue("@pricehcmc", pricehcmc);
                return comn.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                Connection.CloseConnection();
            }

        }
        public DataTable LoadP()
        {
            con = Connection.StartConnection();
            comn = new SqlCommand("ViewProvince", con);
            comn.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(comn);

            ds = new DataSet();
            try
            {
                da.Fill(ds, "province");
                return ds.Tables["province"];
            }
            catch
            {
                throw;
            }
            finally
            {
                ds.Dispose();
                da.Dispose();
                con.Close();
                con.Dispose();
            }
        }

        public int Delete_Province(int provinceID)
        {
            con = Connection.StartConnection();
            con.Open();
            comn = new SqlCommand("DeletePro", con);
            comn.CommandType = CommandType.StoredProcedure;
            try
            {
                comn.Parameters.AddWithValue("@provinceID", provinceID);
                return comn.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }
            finally
            {
                comn.Dispose();
                con.Close();
                con.Dispose();
            }
        }


       
    }
}
